/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package entity;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.enterprise.context.SessionScoped;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.inject.Named;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;

/**
 * User ist eine Datenank welche die Benutzer des Wikis beschreibt. 
 * Hier werden Name, Password und die Information gespeichert ob der User
 * ein Administrator ist oder nicht.
 * 
 * @author Patrick Kuhlmann
 */
@SessionScoped
@Entity
@Table(name="WIKI_USER")
@Named
public class User implements Serializable{
    @Id @GeneratedValue(strategy = GenerationType.AUTO)
    //private int id;
    private String user_name;
    private String user_password;
    private boolean boss;
    private boolean basisuser;
    
    @ManyToMany( cascade = CascadeType.ALL)
    private List<Groups> group;
    
    @OneToMany( mappedBy = "creator", cascade = CascadeType.ALL )
    private List<Post> posts;
    
    public User(String name, String password) {
        this.user_name = name;
        this.user_password = password;
        group = new ArrayList();
    }

    public User() {
        group = new ArrayList();
    }
    
    public void readUser(){
        if (user_name == null){
            ExternalContext ec = FacesContext.getCurrentInstance().getExternalContext();
            user_name = ec.getRemoteUser();
            if(ec.isUserInRole("boss")){
                boss = true;
            }
            if(ec.isUserInRole("basisuser")){
                basisuser = true;
            }
        }
    }

    public boolean isBasisuser() {
        readUser();
        return basisuser;
    }
    public boolean isBoss() {
        readUser();
        return boss;
    }
    
      public boolean isUser() {
        readUser();
        return (boss||basisuser);
    }

    public void setBoss(boolean admin) {
        this.boss = admin;
    }
    public void setBasisuser(boolean basisuser) {
        this.basisuser = basisuser;
    }

    
    public boolean checkUser_Password(String password){
        return this.user_password.equals(password);
    }

    public String getUser_name() {
        return user_name;
    }

    public void setUser_name(String user_name) {
        this.user_name = user_name;
    }

    public String getUser_password() {
        return user_password;
    }

    public void setUser_password(String user_password) {
        this.user_password = user_password;
    }

    public List<Groups> getGroup() {
        return group;
    }

    public void setGroup(List<Groups> group) {
        this.group = group;
    }

    public void addGroup(Groups group) {
        this.group.add(group);
    }
    

    public List<Post> getPosts() {
        return posts;
    }

    public void setPosts(List<Post> posts) {
        this.posts = posts;
    }
    
}
